
import 'package:flutter_ahj_app/app/HttpRequest.dart';
import 'package:flutter_ahj_app/base/basewidget/model/BaseModel.dart';
import 'package:rxdart/rxdart.dart';

class AccountModel extends BaseModel {
  // 修改密码
  BehaviorSubject<Map> _changePasswordObservable;
  Stream<Map> get changePasswordDataStream => _changePasswordObservable.stream;
  changePassword(Map map) {
    _changePasswordObservable =  BehaviorSubject();
    HttpRequest.changePassword(map).listen((result){
      _changePasswordObservable.add(result);
    },onError: (e){
      _changePasswordObservable.add(null);
      errorData.add(e);
    });
  }

  // 用户信息
  BehaviorSubject<Map> _getUserInfoObservable;
  Stream<Map> get getUserInfoDataStream => _getUserInfoObservable.stream;
  getUserInfo() {
    _getUserInfoObservable =  BehaviorSubject();
    HttpRequest.userInfoRequest().listen((result){
      _getUserInfoObservable.add(result);
    },onError: (e){
      errorData.add(e);
    });
  }

  BehaviorSubject<Map> _tenantApproveObservable;
  Stream<Map> get gettenantDataStream => _tenantApproveObservable.stream;
  tenantApprove() {
    _tenantApproveObservable =  BehaviorSubject();
    HttpRequest.tenantApproveRequest().listen((result){
      _tenantApproveObservable.add(result);
    },onError: (e){
      errorData.add(e);
    });
  }


  // 获取验证码
  BehaviorSubject<Map> _getCodeObservable;
  Stream<Map> get getCodeDataStream => _getCodeObservable.stream;
  getCode(Map map) {
    _getCodeObservable =  BehaviorSubject();
    HttpRequest.sendCodeApi(map).listen((result){
      _getCodeObservable.add(result);
    },onError: (e){
      errorData.add(e);
    });
  }

  // 校验验证码
  BehaviorSubject<Map> _checkCodeObservable;
  Stream<Map> get checkCodeDataStream => _checkCodeObservable.stream;
  checkCode(Map map) {
    _checkCodeObservable =  BehaviorSubject();
    HttpRequest.checkCodeApi(map).listen((result){
      _checkCodeObservable.add(result);
    },onError: (e){
      errorData.add(e);
    });
  }

  // 重置密码
  BehaviorSubject<Map> _resetPasswordObservable;
  Stream<Map> get resetPasswordStream => _resetPasswordObservable.stream;
  resetPassword(Map map) {
    _resetPasswordObservable =  BehaviorSubject();
    HttpRequest.resetPasswordApi(map).listen((result){
      _resetPasswordObservable.add(result);
    },onError: (e){
      errorData.add(e);
    });
  }

  // 重置密码
  BehaviorSubject<Map> _loginOutObservable;
  Stream<Map> get loginOutStream => _loginOutObservable.stream;
  loginOut() {
    _loginOutObservable =  BehaviorSubject();
    HttpRequest.loginOutApi().listen((result){
      _loginOutObservable.add(result);
    },onError: (e){
      errorData.add(e);
    });
  }

  // 查询企业信息
  BehaviorSubject<Map> _qyxxObservable;
  Stream<Map> get qyxxStream => _qyxxObservable.stream;
  qyxx() {
    _qyxxObservable =  BehaviorSubject();
    HttpRequest.qyxxApi().listen((result){
      _qyxxObservable.add(result);
    },onError: (e){
      errorData.add(e);
    });
  }

  // 切换身份
  BehaviorSubject<Map> _qhsfyxxObservable;
  Stream<Map> get qhsfStream => _qhsfyxxObservable.stream;
  qhsf(var tenantId) {
    _qhsfyxxObservable =  BehaviorSubject();
    HttpRequest.qhsfApi(tenantId).listen((result){
      _qhsfyxxObservable.add(result);
    },onError: (e){
      errorData.add(e);
    });
  }

  @override
  void dispose() {
    super.dispose();
    if (_changePasswordObservable != null) _changePasswordObservable.close();
    if (_getUserInfoObservable != null) _getUserInfoObservable.close();
    if (_getCodeObservable != null) _getCodeObservable.close();
    if (_checkCodeObservable != null) _checkCodeObservable.close();
    if (_resetPasswordObservable != null) _resetPasswordObservable.close();
    if (_loginOutObservable != null) _loginOutObservable.close();
    if (_tenantApproveObservable != null) _tenantApproveObservable.close();
    if (_qyxxObservable != null) _qyxxObservable.close();
    if (_qhsfyxxObservable != null) _qhsfyxxObservable.close();
  
  }
}
